package shortestpath.bellmanford.graph;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import shortestpath.Edge;
import shortestpath.Graph;
import shortestpath.Node;

public class WorstCaseGraph implements Graph<Node> {

	private class MyNode extends Node.AbstractNodeImpl {
		public MyNode(String name) {
			super(name);
		}
	}

	private final Set<Node> nodes = new HashSet<Node>();
	private final Map<Node, Set<Edge<Node>>> edges = new HashMap<Node, Set<Edge<Node>>>();

	public WorstCaseGraph(final int x) {
		for (int i = 0; i < x; i++) {
			final Node newNode = new MyNode(String.valueOf(i));
			this.nodes.add(newNode);
			this.edges.put(newNode, new HashSet<Edge<Node>>());
		}

		final Random r = new Random();
		for (final Node a : this.nodes) {
			for (final Node b : this.nodes) {
				this.edges.get(a).add(new Edge<Node>(r.nextInt(), a, b));
			}
		}
	}
	@Override
	public Edge<Node> getEdge(Node a, Node b) {
		for (Edge<Node> edge : this.edges.get(a)) {
			if (edge.getEnd().equals(b)) {
				return edge;
			}
		}
		return null;
	}

	@Override
	public Set<Edge<Node>> getEdges() {
		final Set<Edge<Node>> result = new HashSet<Edge<Node>>();
		for (Set<Edge<Node>> edges : this.edges.values()) {
			result.addAll(edges);
		}
		return result;
	}

	@Override
	public Set<Node> getLinkedNodes(Node n) {
		final Set<Node> result = new HashSet<Node>();
		for (Edge<Node> edge : this.edges.get(n)) {
			result.add(edge.getEnd());
		}
		return result;
	}

	@Override
	public Set<Node> getNodes() {
		return this.nodes;
	}

}
